import json
import os
import requests
import GetComment

from absl import app
from Write import XlsWrite

# 这里定义一些需要配置的参数
#  想要爬取的用户名
use_name = '黎娜娜'
#  想要配置的用户ID
useid = '3558929181'
file_path = os.getcwd() + '\\{}'.format(use_name)


def get_long_weibo():
    hd = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
          'Accept-Encoding': 'gzip, deflate, br',
          'Connection': 'keep-alive',
          'Host': 'm.weibo.cn',
          'Upgrade-Insecure-Requests': '1',
          'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                        'Chrome/86.0.4240.111 Safari/537.36',
          'Cookie': 'SCF=Atl-GJH959wN90W-riRIiBlEjMKixuXaDw6Agb7pCDrl5dQ5QxA9yYy7gs2kUAe5UfJf5xLI4o8wNDtzqVv1K6Q.; '
                    '_T_WM=502528b44065f9c113cffe7a6d82ce82; WEIBOCN_WM=20005_0002; WEIBOCN_FROM=1110006030; '
                    'SSOLoginState=1703406342; ALF=1705998342; XSRF-TOKEN=cfe556; '
                    'SUB=_2A25Ig5tWDeRhGeNI6VAW9S7KzjmIHXVr4JKerDV6PUNbktANLXTykW1NRbTMY28PrqPjjs1wQYcgCZOThtZgd1Tw; '
                    'SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WFBckzH77QBJMIF9zHnKII_5JpX5K-hUgL.Fo-ceozNSK5cSK'
                    '-2dJLoIp7LxKML1KBLBKnLxKqL1hnLBoMfSozES0-7So-f; MLOGIN=1; '
                    'M_WEIBOCN_PARAMS=oid%3D4981396422788511%26luicode%3D20000061%26lfid%3D4981396422788511%26fid'
                    '%3D1005053558929181%26uicode%3D10000011; mweibo_short_token=ef653513ee',
          'User-Agent': 'Mozilla/5.0(Windows NT 10.0;Win64;x64;rv: 89.0) Gecko / 20100101Firefox / 89.0'
          }
    id = '4947689294008844'  # 记得修改这里
    mid = '4947689294008844'  # 记得修改这里  TODO 直接请求一级页面获取这两个值 把第一次请求也变为自动化
    maxid = '0'
    maxtype = '0'
    maxidtype = '0'
    # first_url = 'https://m.weibo.cn/u/{}?jumpfrom=weibocom'.format(useid)
    home_url = 'https://m.weibo.cn/api/container/getIndex?containerid=107603{}'.format(useid)
    print(home_url)
    resp = requests.get(home_url, headers=hd)  # 访问微博首页列表
    # print(resp)
    r_content = resp.content
    m = str(r_content, encoding='utf-8')
    parsed_data = json.loads(m)
    print('parsed_data{}'.format(parsed_data))
    weibo_list = parsed_data['data']['cards']  # 获取微博列表
    scheme = parsed_data['data']['scheme']  # 这个参数里面包含如下  containerid luicode lfid
    since_id = parsed_data['data']['cardlistInfo']['since_id']  # 获取下一次翻页参数
    total = parsed_data['data']['cardlistInfo']['total']  # 获取下总共的微博数目
    # sinaweibo://cardlist?containerid=1076033558929181&oid
    # =4947694008140968&luicode=20000061&lfid=4947694008140968&v_p=42
    #
    # TODO 翻页 https://m.weibo.cn/api/container/getIndex?luicode=20000174&type=uid&value=3558929181&containerid
    #  =1076033558929181&since_id=4977047242019392
    # TODO 如何分辨是爬取第一页？重新架构
    write = XlsWrite(file_path)
    cur_weibo_count = 0
    while (total > 0):
        for weibo in weibo_list:
            cur_weibo_count += 1
            print('weibo{}'.format(weibo))
            # breakpoint()
            scheme = weibo['scheme']  # 具体的微博URL
            id = weibo['mblog']['id']
            mid = weibo['mblog']['mid']
            getComment = GetComment.GetLongWeibo()
            comments = getComment.get_weibo_comment(id, mid)
            write.write_weibo(comments)
        # user_comments = data['data']  # 评论包
        print('weibo_list{}'.format(weibo_list))
        # breakpoint()
        total = total - 10  # 一页获取10个博文 且已经在上一次获取了本次的十页

        next_url = 'https://m.weibo.cn/api/container/getIndex?luicode=20000174&type=uid&value=3558929181&containerid'
        '=1076033558929181&since_id={}'.format(since_id)
        resp = requests.get(next_url, headers=hd)  # 访问微博首页列表
        r_content = resp.content
        m = str(r_content, encoding='utf-8')
        parsed_data = json.loads(m)
        print('parsed_data{}'.format(parsed_data))
        weibo_list = parsed_data['data']['cards']  # 获取微博列表
        since_id = parsed_data['data']['cardlistInfo']['since_id']  # 获取下一次翻页参数
        print('当前爬取微博数目{}'.format(cur_weibo_count))

    # user_comments = user_comments[0]
    # max_id = data['max_id']
    # max_id_type = data['max_id_type']

    index = 0


def main(_):
    get_long_weibo()


if __name__ == '__main__':
    app.run(main)
